﻿using System;
using System.Data;
using System.Windows.Forms;
using ToolUtil;
using zyconfig;

namespace zymain
{
    public partial class FrmLtgzr : Form
    {
        private BindingSource BookData;
        public string uid;
        private string eqCell = string.Empty;
        public FrmLtgzr()
        {
            InitializeComponent();
        }

        private void FrmLtgzr_Load(object sender, EventArgs e)
        {
            DateTime year = DateTime.Now.AddDays(80);
            DateChoser.AddTo(this.sTime);
            DateChoser.AddTo(this.eTime,year);
            DateChoser.AddTo(this.dateTxt);
            this.getGridView(this.dateTxt.Text.Trim());
        }
        private void getGridView(string text)
        {
            FrmWait wait = new FrmWait();
            wait.ShowInTaskbar = false;
            wait.Owner = this;
            wait.Show();
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.SqlCmd = " select convert(char(10),日期,120) as 日期,对外服务,处理过期文献,备注 from 流通工作日 order by 日期";
                DataTable dt = _open.GetDataTable();
                this.BookData = new BindingSource();
                this.BookData.DataSource = dt;
                this.bookGridView.DataSource = this.BookData;
                wait.label4.Text = "获取流通工作日";
                Application.DoEvents();
                for (int i = 0; i < bookGridView.Rows.Count; i++)
                {
                    if (this.bookGridView.Rows[i].Cells[0].Value.Equals(text.Trim()))
                    {
                        this.bookGridView.CurrentCell = this.bookGridView.Rows[i].Cells[0];
                        break;
                    }
                }
            }
            wait.Close();
        }
        private void plBtn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认要批量设置流通工作日吗？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                FrmWait wait = new FrmWait();
                wait.ShowInTaskbar = false;
                wait.Owner = this;
                wait.Show();
                DateTime dt1 = DateTime.Parse(this.sTime.Text.Trim());
                DateTime dt2 = DateTime.Parse(this.eTime.Text.Trim());
                using (DbOperHandler _open = new Common().Doh())
                {
                    while (DateTime.Compare(dt1, dt2) <= 0)
                    {
                        _open.Reset();
                        _open.ConditionExpress = "日期='" + dt1 + "'";
                        if (_open.Exist("流通工作日"))
                        {
                            if (this.fwCheck.Checked)
                                _open.AddFieldItem("对外服务", "Y");
                            else
                                _open.AddFieldItem("对外服务", "N");
                            if (this.cqCheck.Checked)
                                _open.AddFieldItem("处理过期文献", "Y");
                            else
                                _open.AddFieldItem("处理过期文献", "N");
                            _open.Update("流通工作日");
                        }
                        else
                        {
                            _open.Reset();
                            _open.AddFieldItem("日期", dt1);
                            if (this.fwCheck.Checked)
                                _open.AddFieldItem("对外服务", "Y");
                            else
                                _open.AddFieldItem("对外服务", "N");
                            if (this.cqCheck.Checked)
                                _open.AddFieldItem("处理过期文献", "Y");
                            else
                                _open.AddFieldItem("处理过期文献", "N");
                            _open.Insert("流通工作日");
                        }
                        wait.label4.Text = "正在设置 " + dt1;
                        Application.DoEvents();
                        dt1 = dt1.AddDays(1);
                        //MessageBox.Show(dt1.ToString("yyyy-MM-dd"));
                    }
                }
                wait.Close();
                this.getGridView(this.sTime.Text.Trim());
            }
        }

        private void dgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            this.eqCell = this.bookGridView.Rows[e.RowIndex].Cells[1].Value.ToString();
            MessageBox.Show("dgv_CellEndEdit:" + this.eqCell);
        }

        private void dgv_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            this.eqCell = this.bookGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        }

        private void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (this.bookGridView.DataSource != null)
            {
                string str = this.bookGridView.CurrentCell.Value.ToString();
                if (!str.Equals(this.eqCell))
                {
                    string dqrq = this.bookGridView.Rows[e.RowIndex].Cells[0].Value.ToString();
                    using (DbOperHandler _open = new Common().Doh())
                    {
                        _open.Reset();
                        _open.ConditionExpress = " 日期='" + dqrq + "'";
                        if (e.ColumnIndex == 1)
                            _open.AddFieldItem("对外服务", this.bookGridView.CurrentCell.Value.ToString());
                        if (e.ColumnIndex == 2)
                            _open.AddFieldItem("处理过期文献", this.bookGridView.CurrentCell.Value.ToString());
                        _open.Update("流通工作日");
                        //MessageBox.Show("dgv_CellValueChanged:" + this.bookGridView.CurrentCell.Value.ToString() + ":" + e.RowIndex + " " + e.ColumnIndex);
                    }
                    this.getGridView(dqrq.Trim());
                }                    
            }
        }

        private void date_textchanged(object sender, EventArgs e)
        {
            for (int i = 0; i < bookGridView.Rows.Count; i++)
            {
                if (this.bookGridView.Rows[i].Cells[0].Value.Equals(this.dateTxt.Text.Trim()))
                {
                    this.bookGridView.CurrentCell = this.bookGridView.Rows[i].Cells[0];
                }
            }
        }
    }
}
